﻿using System;

using SBPweb.Communication.Shared.Library.Conversations;

using Matrix.Xmpp.Client;

namespace SBPweb.Communication.Client.Library.Xmpp.Conversations
{

    partial class Conversation : ConversationBase
    {

        #region Methods

        public override void SendMessage(string body)
        {
            Message msg = new Message(body);
            msg.message.To = this.Receipient.Id;
            SendMessage(msg);
        }

        public override void SendMessage(MessageBase message)
        {
            Message msg = message as Message;
            if (this.isConference)
            {
                msg.message.Type = Matrix.Xmpp.MessageType.groupchat;
                if (msg.To == null)
                {
                    msg.message.To = this.Receipient.Id;
                }
            }
            else
            {
                msg.message.Type = Matrix.Xmpp.MessageType.chat;
                if (msg.From == null)
                {
                    msg.From = Manager.LocalContact;
                }
                if (msg.To == null && Manager.LocalContact.Id == this.Owner.Id)
                {
                    msg.To = this.Members[1];
                }
            }
            msg.isOwn = true;
            Manager.OnClientMessageSend(this, new MessageBaseEventArgs(message));
        }

        protected internal void ReceiveMessage(Message e)
        {
            OnMessageReceive(this, new MessageBaseEventArgs(e));
        }

        protected internal void SendPresence()
        {
            Presence p = new Presence();
            Matrix.Jid jid = new Matrix.Jid(Receipient.Id);
            jid.Resource = Manager.LocalContact.DisplayName;
            p.To = jid;
            Manager.Connections.client.Send(p);
        }

        #endregion

    }

}